package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.query.DestinationQuery;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("destination")
public class DestinationController {
    @Autowired
    private IDestinationService destinationService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")DestinationQuery qo){
        // toasts（当前位置的显示 层次）
        model.addAttribute("toasts",destinationService.getToasts(qo.getParentId()));

        // page
        Page<Destination> list = destinationService.query(qo);
        model.addAttribute("page",list);
        return "destination/list";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Destination destination){
        if (StringUtils.hasLength(destination.getId())) {
            // 编辑
            // 全量更新（若前端传过来的destination 属性有空 或者 提交表单的时候没有其中一些属性内容，直接更新会覆盖原本的东西）
            destinationService.update(destination);
        }else {
            // 保存
            // destination.setId(null);
            destinationService.save(destination);
        }
        return JsonResult.success();
    }

//    @RequestMapping("/delete")
//    @ResponseBody
//    public Object delete(String id){
//        destinationService.delete(id);
//        return JsonResult.success();
//    }

    // 编辑（简介）
    @RequestMapping("/updateInfo")
    @ResponseBody
    public Object updateInfo(String id, String info){
        destinationService.updateInfo(id,info);
        return JsonResult.success();
    }
}
